﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Drawing;

public static class Common
{
    public static string FirstLetterUppercase(this string str)
    {
        string text = str[0].ToString();
        return text.ToUpper() + str.Substring(1);
    }
    public static string HumpNaming(this string str)
    {
        var arr = str.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
        var name = arr[0].ToLower();
        for (int i = 1; i < arr.Length; i++)
        {
            name += arr[i].FirstLetterUppercase();
        }
        return name;
    }
    public static string PascalNaming(this string str)
    {
        var arr = str.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
        var name = "";
        for (int i = 0; i < arr.Length; i++)
        {
            name += arr[i].FirstLetterUppercase();
        }
        return name;
    }

    public static string GetPropertyName(this string str)
    {
        return "get" + str.FirstLetterUppercase();
    }

    public static string SetPropertyName(this string str)
    {
        return "set" + str.FirstLetterUppercase();
    }

    public static string ExchangeCsharpType(this string str)
    {
        var reg = new Regex("\\(\\d+\\)");
        var txt = reg.Replace(str, "").ToLower();
        switch (txt)
        {
            case "tinyint":
            case "smallint":
            case "mediumint":
            case "integer":
            case "int":
                return "int";
            case "float":
                return "float";
            case "double":
            case "decimal":
                return "double";
            case "date":
            case "datetime":
                return "Date";
            case "bit":
                return "boolean";
            default:
                return "String";
        }
    }
    public static string SetParamType(this string str)
    {
        switch (str)
        {
            case "int":
                return "setInt";
            case "float":
                return "setFloat";
            case "double":
                return "setDouble";
            case "Date":
                return "setTimestamp";
            case "boolean":
                return "setBoolean";
            default:
                return "setString";
        }
    }
    public static string GetParamType(this string str)
    {
        switch (str)
        {
            case "int":
                return "getInt";
            case "float":
                return "getFloat";
            case "double":
                return "getDouble";
            case "Date":
                return "getDate";
            case "boolean":
                return "getBoolean";
            default:
                return "getString";
        }
    }
    public static void SetCodeColor(RichTextBox rtb)
    {
        string[] blueKeywords = new string[] { "package", "import", "public","for","if","else","else if",
                "class", "private", "return","this","void","new","static","null","while","do","try","catch",
                "byte","short","int","long","float","double","boolean","char","return" };
        string[] orangeKeywords = new string[] { "String", "Date", "DBHelper", "SQLException",
        "ArrayList"};
        string[] grayKeywords = new string[] { "(?<!:)\\/\\/.*|\\/\\*(\\s|.)*?\\*\\/" };
        foreach (var key in blueKeywords)
        {
            Regex regex = new Regex(key);
            MatchCollection collection = regex.Matches(rtb.Text);
            foreach (Match match in collection)
            {
                rtb.SelectionStart = match.Index;
                rtb.SelectionLength = match.Length;
                rtb.SelectionColor = Color.Blue;
            }
        }
        foreach (var key in orangeKeywords)
        {
            Regex regex = new Regex(key);
            MatchCollection collection = regex.Matches(rtb.Text);
            foreach (Match match in collection)
            {
                rtb.SelectionStart = match.Index;
                rtb.SelectionLength = match.Length;
                rtb.SelectionColor = Color.Orange;
            }
        }
        foreach (var key in grayKeywords)
        {
            Regex regex = new Regex(key);
            MatchCollection collection = regex.Matches(rtb.Text);
            foreach (Match match in collection)
            {
                rtb.SelectionStart = match.Index;
                rtb.SelectionLength = match.Length;
                rtb.SelectionColor = Color.Gray;
            }
        }
    }
}
